package com.zhangwei.liwushuo.fragment;


import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import com.zhangwei.liwushuo.R;
import com.zhangwei.liwushuo.adapter.CategoryExpAdapter;
import com.zhangwei.liwushuo.bean.AppEntity;
import com.zhangwei.liwushuo.bean.CateGoryGroupEntity;
import com.zhangwei.liwushuo.bean.Data;
import com.zhangwei.liwushuo.config.AppConstants;
import com.zhangwei.liwushuo.ui.SubjectActivity_;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@EFragment(R.layout.fragment_category_strategy)
public class CategoryStrategyFragment extends BaseFragment implements ExpandableListView.OnChildClickListener, ExpandableListView.OnGroupClickListener {
    @ViewById
    ExpandableListView categoryExplv;
    @ViewById
    TextView categoryLookallText;
    @ViewById
    RecyclerView categoryRecycler;

    private List<CateGoryGroupEntity> entities = new ArrayList<>();
    private CategoryExpAdapter adapter;
    private TypeReference<AppEntity<Data>> type = new TypeReference<AppEntity<Data>>() {
    };
    

    @Override
    public void initView() {
        super.initView();
        initExpLv();
    }

    private void initExpLv() {
        adapter = new CategoryExpAdapter(entities, getActivity());
        categoryExplv.setAdapter(adapter);
        categoryExplv.setOnGroupClickListener(this);
        categoryExplv.setOnChildClickListener(this);
    }


    private void initTopPicData() {

    }


    @Override
    public void initData() {
        super.initData();
        AsyncHttpClient client = new AsyncHttpClient();

        client.get(AppConstants.URL_CATEGORY_ALL, new TextHttpResponseHandler() {
            @SuppressWarnings("deprecation")
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @SuppressWarnings("deprecation")
            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                AppEntity<Data> entity = JSONObject.parseObject(responseString, type);
                //noinspection unchecked
                entities.addAll(entity.getData().getCateGoryGroupEntities());
                adapter.notifyDataSetChanged();
                for (int i = 0; i < adapter.getGroupCount(); i++) {
                    categoryExplv.expandGroup(i);
                }
            }
        });
    }

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        return true;
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        return false;
    }

    @Click(R.id.category_lookall_text)
    public void onClick() {
        Intent intent = new Intent(getActivity(), SubjectActivity_.class);
        startActivity(intent);
    }

}
